package com.st.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.st.domain.StCourseChapters;
import com.st.po.CourseChaptersPo;
import com.st.service.IStCourseChaptersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 课程章节Controller
 * 
 * @author zhw
 * @date 2024-10-24
 */
@RestController
@RequestMapping("/st/chapters")
public class StCourseChaptersController extends BaseController
{
    @Autowired
    private IStCourseChaptersService stCourseChaptersService;

    /**
     * 查询课程章节列表
     */
    @PreAuthorize("@ss.hasPermi('st:chapters:list')")
    @GetMapping("/list")
    public TableDataInfo list(StCourseChapters stCourseChapters)
    {
        startPage();
        List<StCourseChapters> list = stCourseChaptersService.selectStCourseChaptersList(stCourseChapters);
        return getDataTable(list);
    }

    /**
     * 导出课程章节列表
     */
    @PreAuthorize("@ss.hasPermi('st:chapters:export')")
    @Log(title = "课程章节", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StCourseChapters stCourseChapters)
    {
        List<StCourseChapters> list = stCourseChaptersService.selectStCourseChaptersList(stCourseChapters);
        ExcelUtil<StCourseChapters> util = new ExcelUtil<StCourseChapters>(StCourseChapters.class);
        util.exportExcel(response, list, "课程章节数据");
    }

    /**
     * 获取课程章节详细信息
     */
    @PreAuthorize("@ss.hasPermi('st:chapters:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(stCourseChaptersService.selectStCourseChaptersById(id));
    }

    /**
     * 新增课程章节
     */
    @PreAuthorize("@ss.hasPermi('st:chapters:add')")
    @Log(title = "课程章节", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StCourseChapters stCourseChapters)
    {
        return toAjax(stCourseChaptersService.insertStCourseChapters(stCourseChapters));
    }

    /**
     * 修改课程章节
     */
    @PreAuthorize("@ss.hasPermi('st:chapters:edit')")
    @Log(title = "课程章节", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StCourseChapters stCourseChapters)
    {
        return toAjax(stCourseChaptersService.updateStCourseChapters(stCourseChapters));
    }

    /**
     * 删除课程章节
     */
    @PreAuthorize("@ss.hasPermi('st:chapters:remove')")
    @Log(title = "课程章节", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(stCourseChaptersService.deleteStCourseChaptersByIds(ids));
    }

    /**
     * 新增课程章节
     */
    @PreAuthorize("@ss.hasPermi('st:chapters:add')")
    @PostMapping("add/courseChapters")
    public AjaxResult add(@RequestBody CourseChaptersPo courseChaptersPo)
    {
        return toAjax(stCourseChaptersService.insertStCourseChapters1(courseChaptersPo));
    }

    /*
    3.获取当前课程已上传的章节信息
     */
    @GetMapping("/listByCourseId")
    public AjaxResult list(@RequestParam("courseId") Integer courseId)
    {
        QueryWrapper<StCourseChapters> qw1 = new QueryWrapper<>();
        qw1.eq("course_id",courseId);
        List<StCourseChapters> courseChapters = stCourseChaptersService.list(qw1);  //章节列表

        return success(courseChapters);
    }
}
